﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using SIS.BLL;
using SIS.ENT;

public partial class student_attendance : System.Web.UI.Page
{
    private string CurrentTimeTableSessionName = "CurrentTimeTableSessionName";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session[CurrentTimeTableSessionName] = null;

            if (Page.Request.Params["ttid"] != null && Page.Request.Params["clsrmid"] != null)
            {
                ShowTimeTable(Convert.ToInt32(Request.Params["ttid"].ToString()), Convert.ToInt32(Request.Params["clsrmid"].ToString()));
            }
            else if (Page.Request.Params["ttid"] != null)
            {
                ShowTimeTable(Convert.ToInt32(Request.Params["ttid"].ToString()), 0);
            }
            else
            {
                //Response.Redirect("timetable.aspx");
            }

            lblTopMessage.Text = "Please seleact a schedule for attendance listing.";
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    private void ShowTimeTable(Int32 TimeTableKey, Int32 ClassRoomKey)
    {
        DataTable _dtTimeTable = new DataTable();
        _dtTimeTable = TimeTableManager.GetItemTable(TimeTableKey);

        if (_dtTimeTable != null && _dtTimeTable.Rows.Count > 0)
        {
            lblTimeTableKey.Text = TimeTableKey.ToString();
            lblSchoolKey.Text = _dtTimeTable.Rows[0]["SchoolKey"].ToString();
            lblLocationKey.Text = _dtTimeTable.Rows[0]["LocationKey"].ToString();
            lblSchoolMajorKey.Text = _dtTimeTable.Rows[0]["MajorKey"].ToString();
            lblStandardLevelKey.Text = _dtTimeTable.Rows[0]["StandardLevelKey"].ToString();

            lblEduPeriodKey.Text = _dtTimeTable.Rows[0]["EducationPeriodlKey"].ToString();
            lblSchoolName.Text = _dtTimeTable.Rows[0]["SchoolName"].ToString();
            lblPeriodDesc.Text = _dtTimeTable.Rows[0]["PeriodDesc"].ToString() + " (" + _dtTimeTable.Rows[0]["EducationPeriodCode"].ToString() + ")";
            lblPeriodDuration.Text = Convert.ToDateTime(_dtTimeTable.Rows[0]["PeriodStartDate"].ToString()).ToString("dd/MMM/yyyy") + " to " + Convert.ToDateTime(_dtTimeTable.Rows[0]["PeriodEndDate"].ToString()).ToString("dd/MMM/yyyy");

            lblMajor.Text = _dtTimeTable.Rows[0]["MajorDesc"].ToString();

            lblStandardLevel.Text = _dtTimeTable.Rows[0]["StandardLevelDesc"].ToString();

            lblLocation.Text = _dtTimeTable.Rows[0]["LocationName"].ToString();
            lblDayGroupDesc.Text = _dtTimeTable.Rows[0]["DayGroupDesc"].ToString();
            lblDayDecriptionList.Text = _dtTimeTable.Rows[0]["DayDescriptionList"].ToString();


            string[] strCurrentDayList = lblDayDecriptionList.Text.Split(',');
            for (int d = 0; d < strCurrentDayList.Length; d++)
            {
                if (strCurrentDayList[d].Trim().ToUpper() == "MONDAY")
                {
                    gvTimeTableDetails.Columns[1].Visible = true;
                }
                else if (strCurrentDayList[d].Trim().ToUpper() == "TUESDAY")
                {
                    gvTimeTableDetails.Columns[2].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "WEDNESDAY")
                {
                    gvTimeTableDetails.Columns[3].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "THURSDAY")
                {
                    gvTimeTableDetails.Columns[4].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "FRIDAY")
                {
                    gvTimeTableDetails.Columns[5].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "SATURDAY")
                {
                    gvTimeTableDetails.Columns[6].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "SUNDAY")
                {
                    gvTimeTableDetails.Columns[7].Visible = true;
                }
            }

            DateTime _dtPeriodStartDate = Convert.ToDateTime(_dtTimeTable.Rows[0]["PeriodStartDate"].ToString());
            DateTime _dtPeriodEndDate = Convert.ToDateTime(_dtTimeTable.Rows[0]["PeriodEndDate"].ToString());

            while (DateTime.Compare(_dtPeriodStartDate, _dtPeriodEndDate) <= 0 && DateTime.Compare(_dtPeriodStartDate, DateTime.Now) <= 0)
            {
                if (lblDayDecriptionList.Text.ToUpper().Contains(_dtPeriodStartDate.ToString("dddd").ToUpper()))
                {
                    ddlAttDate.Items.Add(new ListItem(_dtPeriodStartDate.ToString("dd/MMM/yyyy - dddd"), _dtPeriodStartDate.ToString("dd/MMM/yyyy")));
                }
                _dtPeriodStartDate = _dtPeriodStartDate.AddDays(1);
            }

            ddlAttDate.Text = DateTime.Now.ToString("dd/MMM/yyyy");

            #region TESTING
            //DateTime _dtNow = DateTime.Now;

            //if (DateTime.Compare(_dtNow, _dtPeriodEndDate) < 0)
            //{
            //    while (DateTime.Compare(_dtNow, _dtPeriodStartDate) > 0)
            //    {
            //        _dtNow = _dtNow.AddDays(-1);
            //    }

            //    for (int d = 0; d <= 30; d++)
            //    {
            //        ddlAttDate.Items.Add(_dtNow.ToString("dd/MMM/yyyy"));
            //        _dtNow = _dtNow.AddDays(1);
            //        if (DateTime.Compare(_dtNow, _dtPeriodEndDate) > 0)
            //        {
            //            break;
            //        }
            //    }
            //}
            //else
            //{
            //    while (DateTime.Compare(_dtPeriodStartDate, _dtPeriodEndDate) <= 0)
            //    {
            //        ddlAttDate.Items.Add(_dtPeriodStartDate.ToString("dd/MMM/yyyy"));
            //        _dtPeriodStartDate = _dtPeriodStartDate.AddDays(1);
            //        if (DateTime.Compare(_dtPeriodStartDate, _dtPeriodEndDate) == 1)
            //        {
            //            break;
            //        }
            //    }
            //}
            #endregion

            lblStartTime.Text = _dtTimeTable.Rows[0]["StartTime"].ToString();
            lblSectionDuration.Text = _dtTimeTable.Rows[0]["SectionDuration"].ToString();
            lblNoOfSection.Text = _dtTimeTable.Rows[0]["NoOfSection"].ToString();
            lblBreakTimeCount.Text = _dtTimeTable.Rows[0]["BreakTimeNo"].ToString();

            if (lblBreakTimeCount.Text == "0")
            {
                lblBreakTimeDesc.Text = "No Break time";
            }
            else
            {
                lblBreakTimeDesc.Text = "Break Every " + lblBreakTimeCount.Text + " section(s)";
            }
            lblBreakTimeCount.Visible = false;

            TimeSpan _breakTime1Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime1Duration"];
            lblBreakTime1DurationHour.Text = _breakTime1Duration.Hours.ToString();
            lblBreakTime1DurationMin.Text = _breakTime1Duration.Minutes.ToString();

            TimeSpan _breakTime2Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime2Duration"];
            lblBreakTime2DurationHour.Text = _breakTime2Duration.Hours.ToString();
            lblBreakTime2DurationMin.Text = _breakTime2Duration.Minutes.ToString();

            TimeSpan _breakTime3Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime3Duration"];
            lblBreakTime3DurationHour.Text = _breakTime3Duration.Hours.ToString();
            lblBreakTime3DurationMin.Text = _breakTime3Duration.Minutes.ToString();

            TimeSpan _breakTime4Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime4Duration"];
            lblBreakTime4DurationHour.Text = _breakTime4Duration.Hours.ToString();
            lblBreakTime4DurationMin.Text = _breakTime4Duration.Minutes.ToString();

            TimeSpan _breakTime5Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime5Duration"];
            lblBreakTime5DurationHour.Text = _breakTime5Duration.Hours.ToString();
            lblBreakTime5DurationMin.Text = _breakTime5Duration.Minutes.ToString();

            TimeSpan _breakTime6Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime6Duration"];
            lblBreakTime6DurationHour.Text = _breakTime6Duration.Hours.ToString();
            lblBreakTime6DurationMin.Text = _breakTime6Duration.Minutes.ToString();

            TimeSpan _breakTime7Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime7Duration"];
            lblBreakTime7DurationHour.Text = _breakTime7Duration.Hours.ToString();
            lblBreakTime7DurationMin.Text = _breakTime7Duration.Minutes.ToString();

            TimeSpan _breakTime8Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime8Duration"];
            lblBreakTime8DurationHour.Text = _breakTime8Duration.Hours.ToString();
            lblBreakTime8DurationMin.Text = _breakTime8Duration.Minutes.ToString();

            TimeSpan _breakTime9Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime9Duration"];
            lblBreakTime9DurationHour.Text = _breakTime9Duration.Hours.ToString();
            lblBreakTime9DurationMin.Text = _breakTime9Duration.Minutes.ToString();

            TimeSpan _breakTime10Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime10Duration"];
            lblBreakTime10DurationHour.Text = _breakTime10Duration.Hours.ToString();
            lblBreakTime10DurationMin.Text = _breakTime10Duration.Minutes.ToString();

            lblModifiedDateTime.Text = Convert.ToDateTime(_dtTimeTable.Rows[0]["ModifiedDateTime"].ToString()).ToString("dd/MMM/yyyy hh:mm tt");

            GetDayList();
            GetTimeList();
            ddlClassRoom.DataBind();

            if (ddlClassRoom.Items.Count > 0)
            {
                if (ClassRoomKey > 0)
                {
                    ddlClassRoom.Text = ClassRoomKey.ToString();
                }
                else
                {
                    ddlClassRoom.SelectedIndex = 0;
                }
                ddlClassRoom_SelectedIndexChanged(null, null);
            }
        }
    }

    private DataTable GenerateTimeTableDetails(DataTable dtTimeTableDetail)
    {
        DataTable myTTDetails = new DataTable("tblTimeTableDetails");

        myTTDetails.Columns.Add("StartTimeAndDay");

        string _startTime = lblStartTime.Text;
        int _hour = Convert.ToInt32(_startTime.Substring(0, 2));
        int _min = Convert.ToInt32(_startTime.Substring(3, 2));
        TimeSpan tsStartTime = new TimeSpan(_hour, _min, 0);
        TimeSpan tsNextTime = tsStartTime;

        string _strSectionDuration = lblSectionDuration.Text;
        _hour = Convert.ToInt32(_strSectionDuration.Substring(0, 2));
        _min = Convert.ToInt32(_strSectionDuration.Substring(3, 2));
        TimeSpan tsSectionDuration = new TimeSpan(_hour, _min, 0);

        TimeSpan tsTempBT = new TimeSpan(0, 15, 0);

        int _breakTime = Convert.ToInt32(lblBreakTimeCount.Text);


        string[] strDayList = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

        if (strDayList != null && strDayList.Length > 0)
        {
            for (int d = 0; d < strDayList.Length; d++)
            {
                myTTDetails.Columns.Add(strDayList[d].ToString().Trim());
            }
        }

        myTTDetails.Columns.Add("SectionNo");

        int _rowCount = Convert.ToInt32(lblNoOfSection.Text);

        int _currentBTCount = 0;
        int _currentBTNo = 1;
        int _currentSectionNo = 0;
        string _ItemDesc = string.Empty;

        bool _IsBreakTimeIgnore = false;

        for (int r = 0; r < _rowCount; r++)
        {
            DataRow _dr = myTTDetails.NewRow();
            _IsBreakTimeIgnore = false;
            if (_currentBTCount == _breakTime && _breakTime > 0)
            {
                if (_currentBTNo == 1)
                {
                    _hour = Convert.ToInt32(lblBreakTime1DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime1DurationMin.Text);
                }
                else if (_currentBTNo == 2)
                {
                    _hour = Convert.ToInt32(lblBreakTime2DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime2DurationMin.Text);
                }
                else if (_currentBTNo == 3)
                {
                    _hour = Convert.ToInt32(lblBreakTime3DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime3DurationMin.Text);
                }
                else if (_currentBTNo == 4)
                {
                    _hour = Convert.ToInt32(lblBreakTime4DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime4DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _dr[0] = "Break Time (" + tsTempBT.Hours.ToString("00") + ":" + tsTempBT.Minutes.ToString("00") + ")";
                }
                else if (_currentBTNo == 5)
                {
                    _hour = Convert.ToInt32(lblBreakTime5DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime5DurationMin.Text);
                }
                else if (_currentBTNo == 6)
                {
                    _hour = Convert.ToInt32(lblBreakTime6DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime6DurationMin.Text);
                }
                else if (_currentBTNo == 7)
                {
                    _hour = Convert.ToInt32(lblBreakTime7DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime7DurationMin.Text);
                }
                else if (_currentBTNo == 8)
                {
                    _hour = Convert.ToInt32(lblBreakTime8DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime8DurationMin.Text);
                }
                else if (_currentBTNo == 9)
                {
                    _hour = Convert.ToInt32(lblBreakTime9DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime9DurationMin.Text);
                }
                else if (_currentBTNo == 10)
                {
                    _hour = Convert.ToInt32(lblBreakTime10DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime10DurationMin.Text);
                }

                if (_hour == 0 && _min == 0)
                {
                    _IsBreakTimeIgnore = true;
                }
                else
                {
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _dr[0] = "Break Time (" + tsTempBT.Hours.ToString("00") + ":" + tsTempBT.Minutes.ToString("00") + ")";

                    tsNextTime += tsTempBT;
                }


                _currentBTNo += 1;
            }
            else
            {
                _currentSectionNo += 1;
                _startTime = tsNextTime.ToString();
                _dr[0] = tsNextTime.Hours.ToString("00") + ":" + tsNextTime.Minutes.ToString("00");
                tsNextTime += tsSectionDuration;
                _dr[0] += " to " + tsNextTime.Hours.ToString("00") + ":" + tsNextTime.Minutes.ToString("00");
            }

            for (int d = 0; d < strDayList.Length; d++)
            {
                if (_currentBTCount == _breakTime)
                {
                    if (!_IsBreakTimeIgnore)
                    {
                        _dr[d + 1] = "BreakTime";
                    }
                }
                else
                {

                    if (dtTimeTableDetail != null && dtTimeTableDetail.Rows.Count > 0)
                    {
                        DataRow[] _selectRows = dtTimeTableDetail.Select("SectionNo = " + _currentSectionNo.ToString() + " AND DayName = '" + strDayList[d].ToString() + "'");
                        if (_selectRows != null && _selectRows.Length > 0)
                        {
                            _dr[d + 1] = _selectRows[0]["SubjectName"].ToString() + "<br/>(" + _selectRows[0]["FirstName"].ToString() + " " + _selectRows[0]["LastName"].ToString() + ")";
                        }
                        else
                        {
                            _dr[d + 1] = "Create New";
                        }
                    }
                    else
                    {
                        _dr[d + 1] = "Create New";
                    }
                }
            }

            if (_currentBTCount == _breakTime)
            {
                _currentBTCount = 0;
                r--;
            }
            else
            {
                _currentBTCount += 1;
            }

            _dr[strDayList.Length + 1] = _currentSectionNo.ToString();

            if (!_IsBreakTimeIgnore)
            {
                myTTDetails.Rows.Add(_dr);
            }
        }

        return myTTDetails;
    }

    private void GetTimeList()
    {
        ddlTimeDesc.Items.Clear();
        ddlStartTime.Items.Clear();
        ddlEndTime.Items.Clear();

        string _startTime = lblStartTime.Text;
        int _hour = Convert.ToInt32(_startTime.Substring(0, 2));
        int _min = Convert.ToInt32(_startTime.Substring(3, 2));
        TimeSpan tsStartTime = new TimeSpan(_hour, _min, 0);
        TimeSpan tsNextTime = tsStartTime;

        string _strSectionDuration = lblSectionDuration.Text;
        _hour = Convert.ToInt32(_strSectionDuration.Substring(0, 2));
        _min = Convert.ToInt32(_strSectionDuration.Substring(3, 2));
        TimeSpan tsSectionDuration = new TimeSpan(_hour, _min, 0);

        TimeSpan tsTempBT = new TimeSpan(0, 15, 0);

        int _breakTime = Convert.ToInt32(lblBreakTimeCount.Text);

        int _rowCount = Convert.ToInt32(lblNoOfSection.Text);

        int _currentBTCount = 0;
        int _currentBTNo = 1;
        int _currentSectionNo = 0;
        string _ItemDesc = string.Empty;
        bool _IsBreakTimeIgnore = false;
        for (int r = 0; r < _rowCount; r++)
        {
            tsStartTime = tsNextTime;
            _IsBreakTimeIgnore = false;
            if (_currentBTCount == _breakTime && _breakTime > 0)
            {
                if (_currentBTNo == 1)
                {
                    _hour = Convert.ToInt32(lblBreakTime1DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime1DurationMin.Text);
                }
                else if (_currentBTNo == 2)
                {
                    _hour = Convert.ToInt32(lblBreakTime2DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime2DurationMin.Text);
                }
                else if (_currentBTNo == 3)
                {
                    _hour = Convert.ToInt32(lblBreakTime3DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime3DurationMin.Text);
                }
                else if (_currentBTNo == 4)
                {
                    _hour = Convert.ToInt32(lblBreakTime4DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime4DurationMin.Text);
                }
                else if (_currentBTNo == 5)
                {
                    _hour = Convert.ToInt32(lblBreakTime5DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime5DurationMin.Text);
                }
                else if (_currentBTNo == 6)
                {
                    _hour = Convert.ToInt32(lblBreakTime6DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime6DurationMin.Text);
                }
                else if (_currentBTNo == 7)
                {
                    _hour = Convert.ToInt32(lblBreakTime7DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime7DurationMin.Text);
                }
                else if (_currentBTNo == 8)
                {
                    _hour = Convert.ToInt32(lblBreakTime8DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime8DurationMin.Text);
                }
                else if (_currentBTNo == 9)
                {
                    _hour = Convert.ToInt32(lblBreakTime9DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime9DurationMin.Text);
                }
                else if (_currentBTNo == 10)
                {
                    _hour = Convert.ToInt32(lblBreakTime10DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime10DurationMin.Text);
                }

                if (_hour == 0 && _min == 0)
                {
                    _IsBreakTimeIgnore = true;
                }
                else
                {
                    tsTempBT = new TimeSpan(_hour, _min, 0);
                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";
                    tsNextTime += tsTempBT;
                }

                _currentBTNo += 1;
            }
            else
            {
                _ItemDesc = tsNextTime.ToString();
                tsNextTime += tsSectionDuration;
                _ItemDesc += " to " + tsNextTime.ToString();
            }

            //ddlTimeDesc.Items.Add(new ListItem(_ItemDesc, _currentSectionNo.ToString()));
            //ddlStartTime.Items.Add(new ListItem(tsStartTime.ToString(), tsStartTime.ToString()));
            //ddlEndTime.Items.Add(new ListItem(tsNextTime.ToString(), tsNextTime.ToString()));

            if (_currentBTCount == _breakTime)
            {
                _currentBTCount = 0;
                r--;
            }
            else
            {
                _currentSectionNo += 1;

                if (!_IsBreakTimeIgnore)
                {
                    ddlTimeDesc.Items.Add(new ListItem(_ItemDesc, _currentSectionNo.ToString()));
                    ddlStartTime.Items.Add(new ListItem(tsStartTime.ToString(), tsStartTime.ToString()));
                    ddlEndTime.Items.Add(new ListItem(tsNextTime.ToString(), tsNextTime.ToString()));
                }

                _currentBTCount += 1;
            }
        }
    }

    private void GetDayList()
    {
        ddlDayCode.Items.Clear();

        string[] strDayList = lblDayDecriptionList.Text.Split(',');
        if (strDayList != null && strDayList.Length > 0)
        {
            for (int d = 0; d < strDayList.Length; d++)
            {
                string _strDayName = strDayList[d].ToLower().Trim();

                if (_strDayName == "monday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "1"));
                }
                else if (_strDayName == "tuesday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "2"));
                }
                else if (_strDayName == "wednesday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "3"));
                }
                else if (_strDayName == "thursday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "4"));
                }
                else if (_strDayName == "friday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "5"));
                }
                else if (_strDayName == "saturday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "6"));
                }
                else if (_strDayName == "sunday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "7"));
                }
            }
        }
    }

    /*
    private void ShowTimeTable(Int32 TimeTableKey, Int32 ClassRoomKey)
    {
        DataTable _dtTimeTable = new DataTable();
        _dtTimeTable = TimeTableManager.GetItemTable(TimeTableKey);

        if (_dtTimeTable != null && _dtTimeTable.Rows.Count > 0)
        {
            lblTimeTableKey.Text = TimeTableKey.ToString();
            lblSchoolKey.Text = _dtTimeTable.Rows[0]["SchoolKey"].ToString();
            lblLocationKey.Text = _dtTimeTable.Rows[0]["LocationKey"].ToString();
            lblSchoolMajorKey.Text = _dtTimeTable.Rows[0]["MajorKey"].ToString();
            lblStandardLevelKey.Text = _dtTimeTable.Rows[0]["StandardLevelKey"].ToString();
            lblEduPeriodKey.Text = _dtTimeTable.Rows[0]["EducationPeriodlKey"].ToString();
            lblSchoolName.Text = _dtTimeTable.Rows[0]["SchoolName"].ToString();
            lblPeriodDesc.Text = _dtTimeTable.Rows[0]["PeriodDesc"].ToString() + " (" + _dtTimeTable.Rows[0]["EducationPeriodCode"].ToString() + ")";
            lblPeriodDuration.Text = Convert.ToDateTime(_dtTimeTable.Rows[0]["PeriodStartDate"].ToString()).ToString("dd/MMM/yyyy") + " to " + Convert.ToDateTime(_dtTimeTable.Rows[0]["PeriodEndDate"].ToString()).ToString("dd/MMM/yyyy");

            DateTime _dtPeriodStartDate = Convert.ToDateTime(_dtTimeTable.Rows[0]["PeriodStartDate"].ToString());
            DateTime _dtPeriodEndDate = Convert.ToDateTime(_dtTimeTable.Rows[0]["PeriodEndDate"].ToString());

            while (DateTime.Compare(_dtPeriodStartDate, _dtPeriodEndDate) <= 0 && DateTime.Compare(_dtPeriodStartDate, DateTime.Now) <= 0)
            {
                ddlAttDate.Items.Add(_dtPeriodStartDate.ToString("dd/MMM/yyyy"));
                _dtPeriodStartDate = _dtPeriodStartDate.AddDays(1);
            }

            #region TESTING
            //DateTime _dtNow = DateTime.Now;

            //if (DateTime.Compare(_dtNow, _dtPeriodEndDate) < 0)
            //{
            //    while (DateTime.Compare(_dtNow, _dtPeriodStartDate) > 0)
            //    {
            //        _dtNow = _dtNow.AddDays(-1);
            //    }

            //    for (int d = 0; d <= 30; d++)
            //    {
            //        ddlAttDate.Items.Add(_dtNow.ToString("dd/MMM/yyyy"));
            //        _dtNow = _dtNow.AddDays(1);
            //        if (DateTime.Compare(_dtNow, _dtPeriodEndDate) > 0)
            //        {
            //            break;
            //        }
            //    }
            //}
            //else
            //{
            //    while (DateTime.Compare(_dtPeriodStartDate, _dtPeriodEndDate) <= 0)
            //    {
            //        ddlAttDate.Items.Add(_dtPeriodStartDate.ToString("dd/MMM/yyyy"));
            //        _dtPeriodStartDate = _dtPeriodStartDate.AddDays(1);
            //        if (DateTime.Compare(_dtPeriodStartDate, _dtPeriodEndDate) == 1)
            //        {
            //            break;
            //        }
            //    }
            //}
            #endregion

            ddlAttDate.Text = DateTime.Now.ToString("dd/MMM/yyyy");
            lblMajor.Text = _dtTimeTable.Rows[0]["MajorDesc"].ToString();

            lblStandardLevel.Text = _dtTimeTable.Rows[0]["StandardLevelDesc"].ToString();

            lblLocation.Text = _dtTimeTable.Rows[0]["LocationName"].ToString();
            lblDayGroupDesc.Text = _dtTimeTable.Rows[0]["DayGroupDesc"].ToString();
            lblDayDecriptionList.Text = _dtTimeTable.Rows[0]["DayDescriptionList"].ToString();

            string[] strCurrentDayList = lblDayDecriptionList.Text.Split(',');
            for (int d = 0; d < strCurrentDayList.Length; d++)
            {
                if (strCurrentDayList[d].Trim().ToUpper() == "MONDAY")
                {
                    gvTimeTableDetails.Columns[1].Visible = true;
                }
                else if (strCurrentDayList[d].Trim().ToUpper() == "TUESDAY")
                {
                    gvTimeTableDetails.Columns[2].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "WEDNESDAY")
                {
                    gvTimeTableDetails.Columns[3].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "THURSDAY")
                {
                    gvTimeTableDetails.Columns[4].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "FRIDAY")
                {
                    gvTimeTableDetails.Columns[5].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "SATURDAY")
                {
                    gvTimeTableDetails.Columns[6].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "SUNDAY")
                {
                    gvTimeTableDetails.Columns[7].Visible = true;
                }
            }


            lblStartTime.Text = _dtTimeTable.Rows[0]["StartTime"].ToString();
            lblSectionDuration.Text = _dtTimeTable.Rows[0]["SectionDuration"].ToString();
            lblNoOfSection.Text = _dtTimeTable.Rows[0]["NoOfSection"].ToString();
            lblBreakTimeCount.Text = _dtTimeTable.Rows[0]["BreakTimeNo"].ToString();

            if (lblBreakTimeCount.Text == "0")
            {
                lblBreakTimeDesc.Text = "No Break time";
            }
            else
            {
                lblBreakTimeDesc.Text = "Break Every " + lblBreakTimeCount.Text + " section(s)";
            }
            lblBreakTimeCount.Visible = false;

            TimeSpan _breakTime1Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime1Duration"];
            lblBreakTime1DurationHour.Text = _breakTime1Duration.Hours.ToString();
            lblBreakTime1DurationMin.Text = _breakTime1Duration.Minutes.ToString();

            TimeSpan _breakTime2Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime2Duration"];
            lblBreakTime2DurationHour.Text = _breakTime2Duration.Hours.ToString();
            lblBreakTime2DurationMin.Text = _breakTime2Duration.Minutes.ToString();

            TimeSpan _breakTime3Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime3Duration"];
            lblBreakTime3DurationHour.Text = _breakTime3Duration.Hours.ToString();
            lblBreakTime3DurationMin.Text = _breakTime3Duration.Minutes.ToString();

            TimeSpan _breakTime4Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime4Duration"];
            lblBreakTime4DurationHour.Text = _breakTime4Duration.Hours.ToString();
            lblBreakTime4DurationMin.Text = _breakTime4Duration.Minutes.ToString();

            TimeSpan _breakTime5Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime5Duration"];
            lblBreakTime5DurationHour.Text = _breakTime5Duration.Hours.ToString();
            lblBreakTime5DurationMin.Text = _breakTime5Duration.Minutes.ToString();

            TimeSpan _breakTime6Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime6Duration"];
            lblBreakTime6DurationHour.Text = _breakTime6Duration.Hours.ToString();
            lblBreakTime6DurationMin.Text = _breakTime6Duration.Minutes.ToString();

            TimeSpan _breakTime7Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime7Duration"];
            lblBreakTime7DurationHour.Text = _breakTime7Duration.Hours.ToString();
            lblBreakTime7DurationMin.Text = _breakTime7Duration.Minutes.ToString();

            TimeSpan _breakTime8Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime8Duration"];
            lblBreakTime8DurationHour.Text = _breakTime8Duration.Hours.ToString();
            lblBreakTime8DurationMin.Text = _breakTime8Duration.Minutes.ToString();

            TimeSpan _breakTime9Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime9Duration"];
            lblBreakTime9DurationHour.Text = _breakTime9Duration.Hours.ToString();
            lblBreakTime9DurationMin.Text = _breakTime9Duration.Minutes.ToString();

            TimeSpan _breakTime10Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime10Duration"];
            lblBreakTime10DurationHour.Text = _breakTime10Duration.Hours.ToString();
            lblBreakTime10DurationMin.Text = _breakTime10Duration.Minutes.ToString();

            lblModifiedDateTime.Text = Convert.ToDateTime(_dtTimeTable.Rows[0]["ModifiedDateTime"].ToString()).ToString("dd/MMM/yyyy hh:mm tt");

            GetDayList();
            GetTimeList();
            ddlClassRoom.DataBind();

            if (ddlClassRoom.Items.Count > 0)
            {
                if (ClassRoomKey > 0)
                {
                    ddlClassRoom.Text = ClassRoomKey.ToString();
                }
                else
                {
                    ddlClassRoom.SelectedIndex = 0;
                }
                ddlClassRoom_SelectedIndexChanged(null, null);
            }
        }
    }

    private DataTable GenerateTimeTableDetails(DataTable dtTimeTableDetail)
    {
        DataTable myTTDetails = new DataTable("tblTimeTableDetails");

        myTTDetails.Columns.Add("StartTimeAndDay");

        string _startTime = lblStartTime.Text;
        int _hour = Convert.ToInt32(_startTime.Substring(0, 2));
        int _min = Convert.ToInt32(_startTime.Substring(3, 2));
        TimeSpan tsStartTime = new TimeSpan(_hour, _min, 0);
        TimeSpan tsNextTime = tsStartTime;

        string _strSectionDuration = lblSectionDuration.Text;
        _hour = Convert.ToInt32(_strSectionDuration.Substring(0, 2));
        _min = Convert.ToInt32(_strSectionDuration.Substring(3, 2));
        TimeSpan tsSectionDuration = new TimeSpan(_hour, _min, 0);

        TimeSpan tsTempBT = new TimeSpan(0, 15, 0);

        int _breakTime = Convert.ToInt32(lblBreakTimeCount.Text);


        string[] strDayList = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

        if (strDayList != null && strDayList.Length > 0)
        {
            for (int d = 0; d < strDayList.Length; d++)
            {
                myTTDetails.Columns.Add(strDayList[d].ToString().Trim());
            }
        }

        //myTTDetails.Columns.Add("TimeTableDetailKey");
        //myTTDetails.Columns.Add("DayCode");
        myTTDetails.Columns.Add("SectionNo");

        int _rowCount = Convert.ToInt32(lblNoOfSection.Text);

        if (_breakTime > 0)
        {
            if (_rowCount % _breakTime > 0)
            {
                _rowCount += _breakTime + 1;
            }
            else
            {
                _rowCount += _breakTime;
            }
        }

        int _currentBTCount = 0;
        int _currentBTNo = 1;
        int _currentSectionNo = 0;
        string _ItemDesc = string.Empty;

        for (int r = 0; r < _rowCount; r++)
        {
            DataRow _dr = myTTDetails.NewRow();

            if (_currentBTCount == _breakTime && _breakTime > 0)
            {
                if (_currentBTNo == 1)
                {
                    _hour = Convert.ToInt32(lblBreakTime1DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime1DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _dr[0] = "Break Time (" + tsTempBT.Hours.ToString("00") + ":" + tsTempBT.Minutes.ToString("00") + ")";
                }
                else if (_currentBTNo == 2)
                {
                    _hour = Convert.ToInt32(lblBreakTime2DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime2DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _dr[0] = "Break Time (" + tsTempBT.Hours.ToString("00") + ":" + tsTempBT.Minutes.ToString("00") + ")";
                }
                else if (_currentBTNo == 3)
                {
                    _hour = Convert.ToInt32(lblBreakTime3DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime3DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _dr[0] = "Break Time (" + tsTempBT.Hours.ToString("00") + ":" + tsTempBT.Minutes.ToString("00") + ")";
                }
                else if (_currentBTNo == 4)
                {
                    _hour = Convert.ToInt32(lblBreakTime4DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime4DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _dr[0] = "Break Time (" + tsTempBT.Hours.ToString("00") + ":" + tsTempBT.Minutes.ToString("00") + ")";
                }
                else if (_currentBTNo == 5)
                {
                    _hour = Convert.ToInt32(lblBreakTime5DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime5DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _dr[0] = "Break Time (" + tsTempBT.Hours.ToString("00") + ":" + tsTempBT.Minutes.ToString("00") + ")";
                }
                else if (_currentBTNo == 6)
                {
                    _hour = Convert.ToInt32(lblBreakTime6DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime6DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _dr[0] = "Break Time (" + tsTempBT.Hours.ToString("00") + ":" + tsTempBT.Minutes.ToString("00") + ")";
                }
                else if (_currentBTNo == 7)
                {
                    _hour = Convert.ToInt32(lblBreakTime7DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime7DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _dr[0] = "Break Time (" + tsTempBT.Hours.ToString("00") + ":" + tsTempBT.Minutes.ToString("00") + ")";
                }
                else if (_currentBTNo == 8)
                {
                    _hour = Convert.ToInt32(lblBreakTime8DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime8DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _dr[0] = "Break Time (" + tsTempBT.Hours.ToString("00") + ":" + tsTempBT.Minutes.ToString("00") + ")";
                }
                else if (_currentBTNo == 9)
                {
                    _hour = Convert.ToInt32(lblBreakTime9DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime9DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _dr[0] = "Break Time (" + tsTempBT.Hours.ToString("00") + ":" + tsTempBT.Minutes.ToString("00") + ")";
                }
                else if (_currentBTNo == 10)
                {
                    _hour = Convert.ToInt32(lblBreakTime10DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime10DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _dr[0] = "Break Time (" + tsTempBT.Hours.ToString("00") + ":" + tsTempBT.Minutes.ToString("00") + ")";
                }

                tsNextTime += tsTempBT;
                _currentBTNo += 1;
            }
            else
            {
                _currentSectionNo += 1;
                _startTime = tsNextTime.ToString();
                _dr[0] = tsNextTime.Hours.ToString("00") + ":" + tsNextTime.Minutes.ToString("00");
                tsNextTime += tsSectionDuration;
                _dr[0] += " to " + tsNextTime.Hours.ToString("00") + ":" + tsNextTime.Minutes.ToString("00");
            }

            for (int d = 0; d < strDayList.Length; d++)
            {
                if (_currentBTCount == _breakTime)
                {
                    _dr[d + 1] = "BreakTime";//+ _currentBTNo.ToString();
                }
                else
                {
                    if (dtTimeTableDetail != null && dtTimeTableDetail.Rows.Count > 0)
                    {
                        DataRow[] _selectRows = dtTimeTableDetail.Select("SectionNo = " + _currentSectionNo.ToString() + " AND DayName = '" + strDayList[d].ToString() + "'");
                        if (_selectRows != null && _selectRows.Length > 0)
                        {
                            _dr[d + 1] = _selectRows[0]["SubjectName"].ToString() + "<br/>(" + _selectRows[0]["FirstName"].ToString() + " " + _selectRows[0]["LastName"].ToString() + ")";
                        }
                        else
                        {
                            _dr[d + 1] = "Create New";
                            //_dr[strDayList.Length + 1] = "-1";
                            //_dr[strDayList.Length + 2] = strDayList[0].ToString();
                            //_dr[strDayList.Length + 3] = _currentSectionNo.ToString();
                        }
                    }
                    else
                    {
                        _dr[d + 1] = "Create New";
                        //_dr[strDayList.Length + 1] = "-1";
                        //_dr[strDayList.Length + 2] = strDayList[0].ToString();
                        //_dr[strDayList.Length + 3] = _currentSectionNo.ToString();
                    }
                }
            }

            //_dr[strDayList.Length + 1] = "-1";
            //_dr[strDayList.Length + 2] = strDayList[0].ToString();
            _dr[strDayList.Length + 1] = _currentSectionNo.ToString();

            if (_currentBTCount == _breakTime)
            {
                _currentBTCount = 0;
            }
            else
            {
                _currentBTCount += 1;
            }

            myTTDetails.Rows.Add(_dr);
        }

        return myTTDetails;
    }

    private void GetTimeList()
    {
        ddlTimeDesc.Items.Clear();
        ddlStartTime.Items.Clear();
        ddlEndTime.Items.Clear();

        string _startTime = lblStartTime.Text;
        int _hour = Convert.ToInt32(_startTime.Substring(0, 2));
        int _min = Convert.ToInt32(_startTime.Substring(3, 2));
        TimeSpan tsStartTime = new TimeSpan(_hour, _min, 0);
        TimeSpan tsNextTime = tsStartTime;

        string _strSectionDuration = lblSectionDuration.Text;
        _hour = Convert.ToInt32(_strSectionDuration.Substring(0, 2));
        _min = Convert.ToInt32(_strSectionDuration.Substring(3, 2));
        TimeSpan tsSectionDuration = new TimeSpan(_hour, _min, 0);

        TimeSpan tsTempBT = new TimeSpan(0, 15, 0);

        int _breakTime = Convert.ToInt32(lblBreakTimeCount.Text);

        int _rowCount = Convert.ToInt32(lblNoOfSection.Text);

        //if (_breakTime > 0)
        //{
        //    if (_rowCount % _breakTime > 0)
        //    {
        //        _rowCount += _breakTime + 1;
        //    }
        //    else
        //    {
        //        _rowCount += _breakTime;
        //    }
        //}


        int _currentBTCount = 0;
        int _currentBTNo = 1;
        int _currentSectionNo = 0;
        string _ItemDesc = string.Empty;

        for (int r = 0; r < _rowCount; r++)
        {
            tsStartTime = tsNextTime;

            if (_currentBTCount == _breakTime && _breakTime > 0)
            {
                if (_currentBTNo == 1)
                {
                    _hour = Convert.ToInt32(lblBreakTime2DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime2DurationMin.Text);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    //lblBreakTime1Duration.Text = "Break Time 1<br/>" + tsNextTime.ToString();
                    //divBreakTime1Duration.Visible = true;
                }
                else if (_currentBTNo == 2)
                {
                    _hour = Convert.ToInt32(lblBreakTime2DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime2DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    //lblBreakTime2Duration.Text = "Break Time 2<br/>" + tsNextTime.ToString();
                    //divBreakTime2Duration.Visible = true;
                }
                else if (_currentBTNo == 3)
                {
                    _hour = Convert.ToInt32(lblBreakTime3DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime3DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    //lblBreakTime3Duration.Text = "Break Time 3<br/>" + tsNextTime.ToString();
                    //divBreakTime3Duration.Visible = true;
                }
                else if (_currentBTNo == 4)
                {
                    _hour = Convert.ToInt32(lblBreakTime4DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime4DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    //lblBreakTime4Duration.Text = "Break Time 4<br/>" + tsNextTime.ToString();
                    //divBreakTime4Duration.Visible = true;
                }
                else if (_currentBTNo == 5)
                {
                    _hour = Convert.ToInt32(lblBreakTime5DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime5DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    //lblBreakTime5Duration.Text = "Break Time 5<br/>" + tsNextTime.ToString();
                    //divBreakTime5Duration.Visible = true;
                }
                else if (_currentBTNo == 6)
                {
                    _hour = Convert.ToInt32(lblBreakTime6DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime6DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    //lblBreakTime6Duration.Text = "Break Time 6<br/>" + tsNextTime.ToString();
                    //divBreakTime6Duration.Visible = true;
                }
                else if (_currentBTNo == 7)
                {
                    _hour = Convert.ToInt32(lblBreakTime7DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime7DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    //lblBreakTime7Duration.Text = "Break Time 7<br/>" + tsNextTime.ToString();
                    //divBreakTime7Duration.Visible = true;
                }
                else if (_currentBTNo == 8)
                {
                    _hour = Convert.ToInt32(lblBreakTime8DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime8DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    //lblBreakTime8Duration.Text = "Break Time 8<br/>" + tsNextTime.ToString();
                    //divBreakTime8Duration.Visible = true;
                }
                else if (_currentBTNo == 9)
                {
                    _hour = Convert.ToInt32(lblBreakTime9DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime9DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    //lblBreakTime9Duration.Text = "Break Time 9<br/>" + tsNextTime.ToString();
                    //divBreakTime9Duration.Visible = true;
                }
                else if (_currentBTNo == 10)
                {
                    _hour = Convert.ToInt32(lblBreakTime10DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime10DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";

                    //lblBreakTime10Duration.Text = "Break Time 10<br/>" + tsNextTime.ToString();
                    //divBreakTime10Duration.Visible = true;
                }

                tsNextTime += tsTempBT;
                _currentBTNo += 1;
            }
            else
            {
                _ItemDesc = tsNextTime.ToString();
                tsNextTime += tsSectionDuration;
                _ItemDesc += " to " + tsNextTime.ToString();
            }

            //ddlTimeDesc.Items.Add(new ListItem(_ItemDesc, _currentSectionNo.ToString()));
            //ddlStartTime.Items.Add(new ListItem(tsStartTime.ToString(), tsStartTime.ToString()));
            //ddlEndTime.Items.Add(new ListItem(tsNextTime.ToString(), tsNextTime.ToString()));

            if (_currentBTCount == _breakTime)
            {
                _currentBTCount = 0;
                r--;
            }
            else
            {
                _currentSectionNo += 1;
                ddlTimeDesc.Items.Add(new ListItem(_ItemDesc, _currentSectionNo.ToString()));
                ddlStartTime.Items.Add(new ListItem(tsStartTime.ToString(), tsStartTime.ToString()));
                ddlEndTime.Items.Add(new ListItem(tsNextTime.ToString(), tsNextTime.ToString()));
                _currentBTCount += 1;
            }
        }
    }

    private void GetDayList()
    {
        ddlDayCode.Items.Clear();

        string[] strDayList = lblDayDecriptionList.Text.Split(',');
        if (strDayList != null && strDayList.Length > 0)
        {
            for (int d = 0; d < strDayList.Length; d++)
            {
                string _strDayName = strDayList[d].ToLower().Trim();

                if (_strDayName == "monday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "1"));
                }
                else if (_strDayName == "tuesday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "2"));
                }
                else if (_strDayName == "wednesday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "3"));
                }
                else if (_strDayName == "thursday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "4"));
                }
                else if (_strDayName == "friday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "5"));
                }
                else if (_strDayName == "saturday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "6"));
                }
                else if (_strDayName == "sunday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "7"));
                }
            }
        }
    }
    */

    protected void btnSubmit_OLD_Click(object sender, EventArgs e)
    {
        try
        {
            TimeTableDetail _ttd = new TimeTableDetail();
            DataTable _dtTTD = TimeTableDetailManager.GetItemByTeacherProfileKey(Convert.ToInt32(lblTimeTableKey.Text), Convert.ToInt32(ddlTeacherName.Text), Convert.ToInt16(ddlDayCode.Text), Convert.ToInt16(ddlTimeDesc.Text));
            if (_dtTTD != null && _dtTTD.Rows.Count > 0)
            {
                if (_dtTTD.Rows[0]["ClassRoomKey"].ToString() != ddlClassRoom.Text)
                {
                    lblMessage.Text = "Selected teacher and subject is found on (" + _dtTTD.Rows[0]["ClassRoomCode"].ToString() + ") " + _dtTTD.Rows[0]["ClassRoomName"].ToString() + "'s schedule at same time.";
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    return;
                }
            }

            _ttd.TimeTableDetailKey = Convert.ToInt32(lblTimeTableDetailKey.Text);
            _ttd.TimeTableKey = Convert.ToInt32(lblTimeTableKey.Text);
            _ttd.DayCode = Convert.ToInt16(ddlDayCode.Text);
            _ttd.ClassRoomKey = Convert.ToInt32(ddlClassRoom.Text);
            _ttd.SubjectKey = Convert.ToInt32(ddlSubjectName.Text);
            _ttd.TeacherProfileKey = Convert.ToInt32(ddlTeacherName.Text);
            _ttd.SectionNo = Convert.ToInt16(ddlTimeDesc.Text);

            int _hour = Convert.ToInt32(ddlStartTime.Text.Substring(0, 2));
            int _min = Convert.ToInt32(ddlStartTime.Text.Substring(3, 2));
            _ttd.StartTime = new TimeSpan(_hour, _min, 0);

            _hour = Convert.ToInt32(ddlEndTime.Text.Substring(0, 2));
            _min = Convert.ToInt32(ddlEndTime.Text.Substring(3, 2));
            _ttd.EndTime = new TimeSpan(_hour, _min, 0);

            _ttd.UserID = 0;
            _ttd.Status = true;

            if (TimeTableDetailManager.Save(_ttd) > 0)
            {
                lblTimeTableDetailKey.Text = _ttd.TimeTableDetailKey.ToString();
                ddlClassRoom_SelectedIndexChanged(sender, e);
                lblMessage.Text = "Saved successfully.";
                lblMessage.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblMessage.Text = "Saving failed.";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
        catch (Exception Ex)
        {
            lblTopMessage.Text = "Error found on btnNew_Click.<br/>" + Ex.Message;
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (lblTimeTableDetailKey.Text == "-1")
        {
            lblTopMessage.Text = "Please select a schedule.";
            return;
        }

        if (lblStudentProfileKey.Text == "-1" || lblAttDetailKey.Text == "-1")
        {
            lblTopMessage.Text = "Please select a student.";
            return;
        }

        StudentAttendanceDetail _stuAttDetail = new StudentAttendanceDetail();

        _stuAttDetail.AttDetailKey = Convert.ToInt64(lblAttDetailKey.Text);
        _stuAttDetail.AttKey = Convert.ToInt64(lblAttKey.Text);
        _stuAttDetail.StudentProfileKey = Convert.ToInt32(lblStudentProfileKey.Text);
        _stuAttDetail.AttendanceTypeCode = Convert.ToInt16(ddlAttType.Text);
        _stuAttDetail.Remark = txtAttRemark.Text;
        if (StudentAttendanceDetailManager.Save(_stuAttDetail) > 0)
        {
            btnNew_Click(sender, e);
            gvStudentList.DataBind();
            lblMessage.Text = "Saved successfully!";
            lblMessage.ForeColor = System.Drawing.Color.Green;
        }

    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        try
        {
            lblAttDetailKey.Text = "-1";
            lblStudentProfileKey.Text = "-1";
            //ddlTimeDesc.SelectedIndex = 0;
            //ddlStartTime.SelectedIndex = 0;
            //ddlEndTime.SelectedIndex = 0;
            //ddlSubjectName.SelectedIndex = 0;
            //ddlTeacherName.SelectedIndex = 0;
            lblMessage.Text = string.Empty;
            lblTopMessage.Text = string.Empty;

            //ddlAttDate.Enabled = false;
            ddlAttType.Enabled = false;
            ddlAttType.SelectedIndex = 0;
            ddlDefaultAttType.SelectedIndex = 0;
            txtDefaultAttRemark.Text = string.Empty;

            txtAttRemark.Enabled = false;
            txtAttRemark.Text = string.Empty;
            btnSubmit.Enabled = false;
            btnDelete.Enabled = false;
            chkUpdateAllSession.Checked = false;
            chkUpdateAllSession.Visible = false;

            gvStudentList.DataBind();
        }
        catch (Exception Ex)
        {

            lblTopMessage.Text = "Error found on btnNew_Click.<br/>" + Ex.Message;
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }

    }

    protected void ddlTimeDesc_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            ddlStartTime.SelectedIndex = ddlTimeDesc.SelectedIndex;
            ddlEndTime.SelectedIndex = ddlTimeDesc.SelectedIndex;
            TimeTableDetail _ttd = new TimeTableDetail();
            _ttd = TimeTableDetailManager.GetItemBySectionNo(Convert.ToInt32(lblTimeTableKey.Text), Convert.ToInt32(ddlClassRoom.Text), Convert.ToInt16(ddlDayCode.Text), Convert.ToInt16(ddlTimeDesc.Text));
            if (_ttd != null && _ttd.TimeTableDetailKey > 0)
            {
                lblTopMessage.Text = "Found a schedule on selected day/time.";
                lblTopMessage.ForeColor = System.Drawing.Color.Green;
                lblTimeTableDetailKey.Text = _ttd.TimeTableDetailKey.ToString();
                ddlSubjectName.DataBind();
                if (ddlSubjectName.Items.Count > 0)
                {
                    ddlSubjectName.Text = _ttd.SubjectKey.ToString();
                    ddlTeacherName.DataBind();
                    if (ddlTeacherName.Items.Count > 0)
                    {
                        ddlTeacherName.Text = _ttd.TeacherProfileKey.ToString();
                    }

                }
                btnGenerateList.Enabled = true;
                btnDeleteList.Enabled = true;
            }
            else
            {
                lblTopMessage.Text = "Create a new schedule on selected day/time.";
                lblTopMessage.ForeColor = System.Drawing.Color.Red;
                btnGenerateList.Enabled = false;
                btnDeleteList.Enabled = false;
            }

            //ddlAttDate_SelectedIndexChanged(sender, e);
            ShowAttendance();
        }
        catch (Exception Ex)
        {
            lblTopMessage.Text = "Error found on ddlTimeDesc_SelectedIndexChanged.<br/>" + Ex.Message;
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }

    }

    protected void ShowAttendance()
    {
        btnNew_Click(null, null);
        StudentAttendanceTable _stuAtt = new StudentAttendanceTable();
        _stuAtt = StudentAttendanceTableManager.GetItemBySectionNo(Convert.ToInt32(lblEduPeriodKey.Text), Convert.ToInt32(ddlClassRoom.Text), Convert.ToInt16(ddlDayCode.Text), Convert.ToDateTime(ddlAttDate.Text), Convert.ToInt16(ddlTimeDesc.Text));
        if (_stuAtt != null && _stuAtt.AttKey > 0)
        {
            lblAttKey.Text = _stuAtt.AttKey.ToString();
            txtDefaultAttRemark.Text = _stuAtt.Remark;
            txtDefaultAttRemark.ReadOnly = true;
            chkUpdateAllSession.Visible = false;
            btnGenerateList.Visible = false;
            btnDeleteList.Visible = true;
            divOption.Visible = false;
            gvStudentList.DataBind();

            lblTopMessage.Text = "Attendance record found.";
            lblTopMessage.ForeColor = System.Drawing.Color.Green;
        }
        else
        {
            lblAttKey.Text = "-1";
            chkUpdateAllSession.Visible = true;
            txtDefaultAttRemark.ReadOnly = false;
            btnGenerateList.Visible = true;
            btnDeleteList.Visible = false;
            divOption.Visible = true;
            gvStudentList.DataBind();

            _stuAtt = null;
            _stuAtt = StudentAttendanceTableManager.GetItemByAttDate(Convert.ToInt32(lblEduPeriodKey.Text), Convert.ToInt32(ddlClassRoom.Text), Convert.ToInt16(ddlDayCode.Text), Convert.ToDateTime(ddlAttDate.Text));
            if (_stuAtt != null && _stuAtt.AttKey > 0)
            {
                chkUpdateAllSession.Visible = false;
            }
            else
            {
                chkUpdateAllSession.Visible = true;
            }
            lblTopMessage.Text = "No attendance record found.<br/>Please generate a attendance list firstly.";
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void ddlClassRoom_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            gvTimeTableDetails.DataSource = null;
            Session[CurrentTimeTableSessionName] = null;
            ddlAttDate_SelectedIndexChanged(sender, e);
        }
        catch (Exception Ex)
        {
            lblTopMessage.Text = "Error found on ddlClassRoom_SelectedIndexChanged.<br/>" + Ex.Message;
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }


    }

    protected void ddlDayCode_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlTimeDesc.Items.Count > 0)
        {
            ddlTimeDesc.SelectedIndex = 0;
            ddlTimeDesc_SelectedIndexChanged(sender, e);
        }
    }

    protected void gvTimeTableDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                #region Monday
                HyperLink _hplMonday = (HyperLink)e.Row.FindControl("hplMonday");
                _hplMonday.Text = DataBinder.Eval(e.Row.DataItem, "Monday").ToString();

                LinkButton _btnMonday = (LinkButton)e.Row.FindControl("btnMonday");
                _btnMonday.CommandName = "UpdateSchedule";
                _btnMonday.CommandArgument = "1," + DataBinder.Eval(e.Row.DataItem, "SectionNo").ToString();
                _btnMonday.ToolTip = "Monday, " + DataBinder.Eval(e.Row.DataItem, "StartTimeAndDay").ToString();
                if (_hplMonday.Text.Trim() != string.Empty)
                {
                    if (_hplMonday.Text == "BreakTime")
                    {
                        _btnMonday.Visible = false;
                        _hplMonday.Visible = true;
                        _hplMonday.ToolTip = _btnMonday.ToolTip;
                    }
                    else
                    {
                        _btnMonday.Text = _hplMonday.Text;
                        if (_hplMonday.Text == "Create New")
                        {
                            _btnMonday.Text = "None";
                            _btnMonday.Enabled = false;
                        }
                    }

                    if (lblAttDateDayName.Text.ToUpper() != "MONDAY")
                    {
                        _btnMonday.Enabled = false;
                    }
                }
                #endregion

                #region Tuesday
                HyperLink _hplTuesday = (HyperLink)e.Row.FindControl("hplTuesday");
                if (DataBinder.Eval(e.Row.DataItem, "Tuesday") != null)
                {
                    _hplTuesday.Text = DataBinder.Eval(e.Row.DataItem, "Tuesday").ToString();
                }

                LinkButton _btnTuesday = (LinkButton)e.Row.FindControl("btnTuesday");
                _btnTuesday.CommandName = "UpdateSchedule";
                _btnTuesday.CommandArgument = "2," + DataBinder.Eval(e.Row.DataItem, "SectionNo").ToString();
                _btnTuesday.ToolTip = "Tuesday, " + DataBinder.Eval(e.Row.DataItem, "StartTimeAndDay").ToString();
                if (_hplTuesday.Text.Trim() != string.Empty)
                {
                    if (_hplTuesday.Text == "BreakTime")
                    {
                        _btnTuesday.Visible = false;
                        _hplTuesday.Visible = true;
                        _hplTuesday.ToolTip = _btnTuesday.ToolTip;
                    }
                    else
                    {
                        _btnTuesday.Text = _hplTuesday.Text;
                        if (_btnTuesday.Text == "Create New")
                        {
                            _btnTuesday.Text = "None";
                            _btnTuesday.Enabled = false;
                        }

                    }

                    if (lblAttDateDayName.Text.ToUpper() != "TUESDAY")
                    {
                        _btnTuesday.Enabled = false;
                    }
                }
                #endregion

                #region Wednesday
                HyperLink _hplWednesday = (HyperLink)e.Row.FindControl("hplWednesday");
                _hplWednesday.Text = DataBinder.Eval(e.Row.DataItem, "Wednesday").ToString();

                LinkButton _btnWednesday = (LinkButton)e.Row.FindControl("btnWednesday");
                _btnWednesday.CommandName = "UpdateSchedule";
                _btnWednesday.CommandArgument = "3," + DataBinder.Eval(e.Row.DataItem, "SectionNo").ToString();
                _btnWednesday.ToolTip = "Wednesday, " + DataBinder.Eval(e.Row.DataItem, "StartTimeAndDay").ToString();

                if (_hplWednesday.Text.Trim() != string.Empty)
                {
                    if (_hplWednesday.Text == "BreakTime")
                    {
                        _btnWednesday.Visible = false;
                        _hplWednesday.Visible = true;
                        _hplWednesday.ToolTip = _btnWednesday.ToolTip;
                    }
                    else
                    {
                        _btnWednesday.Text = _hplWednesday.Text;
                        if (_btnWednesday.Text == "Create New")
                        {
                            _btnWednesday.Text = "None";
                            _btnWednesday.Enabled = false;
                        }
                    }

                    if (lblAttDateDayName.Text.ToUpper() != "WEDNESDAY")
                    {
                        _btnWednesday.Enabled = false;
                    }
                }

                #endregion

                #region Thursday
                HyperLink _hplThursday = (HyperLink)e.Row.FindControl("hplThursday");
                _hplThursday.Text = DataBinder.Eval(e.Row.DataItem, "Thursday").ToString();

                LinkButton _btnThursday = (LinkButton)e.Row.FindControl("btnThursday");
                _btnThursday.CommandName = "UpdateSchedule";
                _btnThursday.CommandArgument = "4," + DataBinder.Eval(e.Row.DataItem, "SectionNo").ToString();
                _btnThursday.ToolTip = "Thursday, " + DataBinder.Eval(e.Row.DataItem, "StartTimeAndDay").ToString();

                if (_hplThursday.Text.Trim() != string.Empty)
                {
                    if (_hplThursday.Text == "BreakTime")
                    {
                        _btnThursday.Visible = false;
                        _hplThursday.Visible = true;
                        _hplThursday.ToolTip = _btnThursday.ToolTip;
                    }
                    else
                    {
                        _btnThursday.Text = _hplThursday.Text;
                        if (_btnThursday.Text == "Create New")
                        {
                            _btnThursday.Text = "None";
                            _btnThursday.Enabled = false;
                        }
                    }

                    if (lblAttDateDayName.Text.ToUpper() != "THURSDAY")
                    {
                        _btnThursday.Enabled = false;
                    }
                }

                #endregion

                #region Friday
                HyperLink _hplFriday = (HyperLink)e.Row.FindControl("hplFriday");
                _hplFriday.Text = DataBinder.Eval(e.Row.DataItem, "Friday").ToString();

                LinkButton _btnFriday = (LinkButton)e.Row.FindControl("btnFriday");
                _btnFriday.CommandName = "UpdateSchedule";
                _btnFriday.CommandArgument = "5," + DataBinder.Eval(e.Row.DataItem, "SectionNo").ToString();
                _btnFriday.ToolTip = "Friday, " + DataBinder.Eval(e.Row.DataItem, "StartTimeAndDay").ToString();

                if (_hplFriday.Text.Trim() != string.Empty)
                {
                    if (_hplFriday.Text == "BreakTime")
                    {
                        _btnFriday.Visible = false;
                        _hplFriday.Visible = true;
                        _hplFriday.ToolTip = _btnFriday.ToolTip;
                    }
                    else
                    {
                        _btnFriday.Text = _hplFriday.Text;
                        if (_btnFriday.Text == "Create New")
                        {
                            _btnFriday.Text = "None";
                            _btnFriday.Enabled = false;
                        }
                    }

                    if (lblAttDateDayName.Text.ToUpper() != "FRIDAY")
                    {
                        _btnFriday.Enabled = false;
                    }
                }
                #endregion

                #region Saturday
                HyperLink _hplSaturday = (HyperLink)e.Row.FindControl("hplSaturday");
                _hplSaturday.Text = DataBinder.Eval(e.Row.DataItem, "Saturday").ToString();

                LinkButton _btnSaturday = (LinkButton)e.Row.FindControl("btnSaturday");
                _btnSaturday.CommandName = "UpdateSchedule";
                _btnSaturday.CommandArgument = "6," + DataBinder.Eval(e.Row.DataItem, "SectionNo").ToString();
                _btnSaturday.ToolTip = "Saturday " + DataBinder.Eval(e.Row.DataItem, "StartTimeAndDay").ToString();

                if (_hplSaturday.Text.Trim() != string.Empty)
                {
                    if (_hplSaturday.Text == "BreakTime")
                    {
                        _btnSaturday.Visible = false;
                        _hplSaturday.Visible = true;
                        _hplSaturday.ToolTip = _btnSaturday.ToolTip;
                    }
                    else
                    {
                        _btnSaturday.Text = _hplSaturday.Text;
                        if (_btnSaturday.Text == "Create New")
                        {
                            _btnSaturday.Text = "None";
                            _btnSaturday.Enabled = false;
                        }
                    }

                    if (lblAttDateDayName.Text.ToUpper() != "SATURDAY")
                    {
                        _btnSaturday.Enabled = false;
                    }
                }

                #endregion

                #region Sunday
                HyperLink _hplSunday = (HyperLink)e.Row.FindControl("hplSunday");
                _hplSunday.Text = DataBinder.Eval(e.Row.DataItem, "Sunday").ToString();

                LinkButton _btnSunday = (LinkButton)e.Row.FindControl("btnSunday");
                _btnSunday.CommandName = "UpdateSchedule";
                _btnSunday.CommandArgument = "7," + DataBinder.Eval(e.Row.DataItem, "SectionNo").ToString();
                _btnSunday.ToolTip = "Sunday " + DataBinder.Eval(e.Row.DataItem, "StartTimeAndDay").ToString();

                if (_hplSunday.Text.Trim() != string.Empty)
                {
                    if (_hplSunday.Text == "BreakTime")
                    {
                        _btnSunday.Visible = false;
                        _hplSunday.Visible = true;
                        _hplSunday.ToolTip = _btnSunday.ToolTip;
                    }
                    else
                    {
                        _btnSunday.Text = _hplSunday.Text;
                        if (_btnSunday.Text == "Create New")
                        {
                            _btnSunday.Text = "None";
                            _btnSunday.Enabled = false;
                        }
                    }

                    if (lblAttDateDayName.Text.ToUpper() != "SUNDAY")
                    {
                        _btnSunday.Enabled = false;
                    }
                }
                #endregion
            }
        }
        catch (Exception Ex)
        {
            lblTopMessage.Text = "Error found on gvTimeTableDetails_RowDataBound.<br/>" + Ex.Message;
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void gvTimeTableDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "UpdateSchedule")
            {
                btnNew_Click(sender, e);
                string _commandArgs = e.CommandArgument.ToString();
                string _dayCode = _commandArgs.Split(',')[0].ToString();
                string _sectionNo = _commandArgs.Split(',')[1].ToString();

                ddlDayCode.Text = _dayCode;
                ddlTimeDesc.Text = _sectionNo;
                ddlTimeDesc_SelectedIndexChanged(sender, null);

                LinkButton _btnLink = (LinkButton)e.CommandSource;

                for (int r = 0; r < gvTimeTableDetails.Rows.Count; r++)
                {
                    for (int c = 0; c < gvTimeTableDetails.Rows[r].Cells.Count; c++)
                    {
                        Control _lastLinkButton = gvTimeTableDetails.Rows[r].Cells[c].FindControl(lblLastSelectedLinkButtonID.Text);

                        if (_lastLinkButton != null)
                        {
                            LinkButton _lnkLast = (LinkButton)_lastLinkButton;

                            if (_lnkLast.Enabled)
                            {
                                _lnkLast.ForeColor = System.Drawing.Color.Blue;
                                _lnkLast.Font.Bold = false;
                            }
                            break;
                        }
                    }
                }

                lblLastSelectedLinkButtonID.Text = _btnLink.ID.ToString();
                _btnLink.Font.Bold = true;
                _btnLink.ForeColor = System.Drawing.Color.Red;


            }


        }
        catch (Exception Ex)
        {
            lblTopMessage.Text = "Invalid section day/time. Not found on timetable.<br/>";
            lblTopMessage.Text += "Error found at gvTimeTableDetails_RowCommand<br/>" + Ex.Message;
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToInt32(lblTimeTableKey.Text) > 0 && Convert.ToInt32(lblTimeTableDetailKey.Text) > 0)
            {
                if (TimeTableDetailManager.Delete(Convert.ToInt32(lblTimeTableKey.Text), Convert.ToInt32(lblTimeTableDetailKey.Text)))
                {
                    btnNew_Click(sender, e);
                    lblMessage.Text = "Deleted successfully!";
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                    ddlClassRoom_SelectedIndexChanged(sender, e);
                }
                else
                {
                    lblMessage.Text = "Deleting failed!";
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                }

            }
            else
            {
                lblMessage.Text = "Please select a schedule to delete!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }

        }
        catch (Exception Ex)
        {
            lblTopMessage.Text = "Error found on btnNew_Click.<br/>" + Ex.Message;
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void btnBackToTimeTableSetup_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/school/timetable.aspx?ttid=" + lblTimeTableKey.Text);
    }

    protected void btnGenerateList_Click(object sender, EventArgs e)
    {
        if (lblTimeTableDetailKey.Text == "-1")
        {
            lblTopMessage.Text = "Please select a schedule.";
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
            return;
        }

        //if (lblStudentProfileKey.Text == "-1")
        //{
        //    lblTopMessage.Text = "Please select a student.";
        //    return;
        //}

        DataTable _dtStudentRegisterList = StudentRegisterationTableManager.GetListByTimeTableKey(Convert.ToInt32(lblSchoolKey.Text), Convert.ToInt32(lblLocationKey.Text), Convert.ToInt32(ddlClassRoom.Text), Convert.ToInt32(lblStandardLevelKey.Text), Convert.ToInt32(lblEduPeriodKey.Text), Convert.ToInt32(lblTimeTableKey.Text));

        if (_dtStudentRegisterList != null && _dtStudentRegisterList.Rows.Count > 0)
        {
            if (chkUpdateAllSession.Checked == false)
            {
                #region "Update One Session"
                StudentAttendanceTable _stuAtt = new StudentAttendanceTable();
                _stuAtt.AttKey = Convert.ToInt32(lblAttKey.Text);
                _stuAtt.ClassRoomKey = Convert.ToInt32(ddlClassRoom.Text);
                _stuAtt.DayCode = Convert.ToInt16(ddlDayCode.Text);
                _stuAtt.EducationPeriodKey = Convert.ToInt32(lblEduPeriodKey.Text);
                _stuAtt.SubjectKey = Convert.ToInt32(ddlSubjectName.Text);
                _stuAtt.TeacherProfileKey = Convert.ToInt32(ddlTeacherName.Text);
                _stuAtt.AttDate = Convert.ToDateTime(ddlAttDate.Text);
                _stuAtt.SectionNo = Convert.ToInt16(ddlTimeDesc.Text);
                _stuAtt.Remark = txtDefaultAttRemark.Text;
                int _hour = Convert.ToInt32(ddlStartTime.Text.Substring(0, 2));
                int _min = Convert.ToInt32(ddlStartTime.Text.Substring(3, 2));
                _stuAtt.StartTime = new TimeSpan(_hour, _min, 0);

                _hour = Convert.ToInt32(ddlEndTime.Text.Substring(0, 2));
                _min = Convert.ToInt32(ddlEndTime.Text.Substring(3, 2));
                _stuAtt.EndTime = new TimeSpan(_hour, _min, 0);

                _stuAtt.UserID = 0;

                if (StudentAttendanceTableManager.Save(_stuAtt) > 0)
                {
                    lblAttKey.Text = _stuAtt.AttKey.ToString();

                    int _saveCount = 0;

                    for (int i = 0; i < _dtStudentRegisterList.Rows.Count; i++)
                    {
                        StudentAttendanceDetail _stuAttDetail = new StudentAttendanceDetail();
                        _stuAttDetail.AttDetailKey = -1;
                        _stuAttDetail.AttKey = _stuAtt.AttKey;
                        _stuAttDetail.AttendanceTypeCode = Convert.ToInt16(ddlDefaultAttType.Text);
                        _stuAttDetail.StudentProfileKey = Convert.ToInt32(_dtStudentRegisterList.Rows[i]["StudentProfileKey"].ToString());
                        if (StudentAttendanceDetailManager.Save(_stuAttDetail) > 0)
                        {
                            _saveCount++;
                        }
                        _stuAttDetail = null;
                    }

                    if (_saveCount == _dtStudentRegisterList.Rows.Count)
                    {
                        //ddlAttDate_SelectedIndexChanged(sender, e);
                        ShowAttendance();
                        lblTopMessage.Text = "Generated successfully!";

                        //gvStudentList.DataBind();
                        //lblTopMessage.ForeColor = System.Drawing.Color.Green;
                        //divOption.Visible = false;
                        //btnGenerateList.Visible = false;
                        //btnDeleteList.Visible = true;
                    }
                    else
                    {
                        lblTopMessage.Text = "Error found. Please generate again later.";
                        lblTopMessage.ForeColor = System.Drawing.Color.Red;
                    }
                    _dtStudentRegisterList = null;
                }
                #endregion
            }
            else
            {
                TimeTableDetailList _ttdList = TimeTableDetailManager.GetListByDayCode(Convert.ToInt32(lblTimeTableKey.Text), Convert.ToInt32(ddlClassRoom.Text), Convert.ToInt16(ddlDayCode.Text));
                if (_ttdList != null && _ttdList.Count > 0)
                {
                    foreach (TimeTableDetail _ttd in _ttdList)
                    {
                        StudentAttendanceTable _stuAtt = new StudentAttendanceTable();
                        _stuAtt.AttKey = -1;
                        _stuAtt.ClassRoomKey = Convert.ToInt32(ddlClassRoom.Text);
                        _stuAtt.DayCode = Convert.ToInt16(ddlDayCode.Text);
                        _stuAtt.EducationPeriodKey = Convert.ToInt32(lblEduPeriodKey.Text);
                        _stuAtt.SubjectKey = _ttd.SubjectKey;
                        _stuAtt.TeacherProfileKey = _ttd.TeacherProfileKey;
                        _stuAtt.AttDate = Convert.ToDateTime(ddlAttDate.Text);
                        _stuAtt.SectionNo = _ttd.SectionNo;
                        _stuAtt.Remark = txtDefaultAttRemark.Text;
                        _stuAtt.StartTime = _ttd.StartTime;
                        _stuAtt.EndTime = _ttd.EndTime;
                        _stuAtt.UserID = 0;

                        if (StudentAttendanceTableManager.Save(_stuAtt) > 0)
                        {
                            //lblAttKey.Text = _stuAtt.AttKey.ToString();

                            int _saveCount = 0;

                            for (int i = 0; i < _dtStudentRegisterList.Rows.Count; i++)
                            {
                                StudentAttendanceDetail _stuAttDetail = new StudentAttendanceDetail();
                                _stuAttDetail.AttDetailKey = -1;
                                _stuAttDetail.AttKey = _stuAtt.AttKey;
                                _stuAttDetail.AttendanceTypeCode = Convert.ToInt16(ddlDefaultAttType.Text);
                                _stuAttDetail.StudentProfileKey = Convert.ToInt32(_dtStudentRegisterList.Rows[i]["StudentProfileKey"].ToString());
                                if (StudentAttendanceDetailManager.Save(_stuAttDetail) > 0)
                                {
                                    _saveCount++;
                                }
                                _stuAttDetail = null;
                            }
                        }
                    }

                    //ddlAttDate_SelectedIndexChanged(sender, e);
                    ShowAttendance();
                }
            }



        }
        else
        {
            lblTopMessage.Text = "No student found!";
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void ddlAttDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            btnNew_Click(sender, e);
            lblAttKey.Text = "-1";
            lblTimeTableDetailKey.Text = "-1";
            lblAttDateDayName.Text = Convert.ToDateTime(ddlAttDate.Text).ToString("dddd");
            if (lblTimeTableKey.Text != "-1" && ddlClassRoom.Items.Count > 0)
            {
                if (gvTimeTableDetails.DataSource == null)
                {
                    DataTable _dtTimeTableByClassRom = null;
                    DataTable _dtTimeTableDetails = null;
                    if (Session[CurrentTimeTableSessionName] != null)
                    {
                        _dtTimeTableDetails = (DataTable)Session[CurrentTimeTableSessionName];
                    }
                    else
                    {
                        _dtTimeTableByClassRom = TimeTableDetailManager.GetListTableByClassRoomKey(Convert.ToInt32(lblTimeTableKey.Text), Convert.ToInt32(ddlClassRoom.Text));
                        _dtTimeTableDetails = GenerateTimeTableDetails(_dtTimeTableByClassRom);
                        Session[CurrentTimeTableSessionName] = _dtTimeTableDetails;
                    }

                    gvTimeTableDetails.DataSource = _dtTimeTableDetails;
                    gvTimeTableDetails.DataBind();

                    btnNextAttDate.Enabled = true;
                    btnPreviousAttDate.Enabled = true;
                    ddlDayCode.Text = ddlDayCode.Items.FindByText(lblAttDateDayName.Text.ToUpper()).Value;
                    ddlDayCode_SelectedIndexChanged(sender, e);
                    ShowAttendance();
                }
                else
                {
                    gvTimeTableDetails.DataBind();
                }
            }
            else
            {
                gvTimeTableDetails.DataSource = null;
            }

            
        }
        catch (Exception Ex)
        {
            lblTopMessage.Text = "Error found on ddlAttDate_SelectedIndexChanged.<br/>" + Ex.Message;
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void gvStudentList_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (gvStudentList.SelectedIndex >= 0)
            {
                btnNew_Click(sender, e);
                int _studentProfileKey = Convert.ToInt32(gvStudentList.SelectedDataKey[0]);
                StudentAttendanceDetail _stuAttDetail = new StudentAttendanceDetail();
                _stuAttDetail = StudentAttendanceDetailManager.GetItemByStudentProfileKey(Convert.ToInt64(lblAttKey.Text), _studentProfileKey);
                if (_stuAttDetail != null && _stuAttDetail.AttDetailKey > 0)
                {
                    lblAttDetailKey.Text = _stuAttDetail.AttDetailKey.ToString();
                    lblStudentProfileKey.Text = _stuAttDetail.StudentProfileKey.ToString();
                    ddlAttType.Text = _stuAttDetail.AttendanceTypeCode.ToString();
                    txtAttRemark.Text = _stuAttDetail.Remark;
                    btnSubmit.Enabled = true;
                    ddlAttType.Enabled = true;
                    txtAttRemark.Enabled = true;
                    btnDelete.Enabled = false;
                }
                else
                {
                    lblTopMessage.Text = "Can not load attendance details. Please try again later.";
                    lblTopMessage.ForeColor = System.Drawing.Color.Red;
                    btnSubmit.Enabled = false;
                    ddlAttType.Enabled = false;
                    txtAttRemark.Enabled = false;
                    btnDelete.Enabled = false;
                }
            }

        }
        catch (Exception Ex)
        {
            lblTopMessage.Text = "Error found on gvStudentList_SelectedIndexChanged.<br/>" + Ex.Message;
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void btnDeleteList_Click(object sender, EventArgs e)
    {
        if (lblAttKey.Text != "-1")
        {
            if (StudentAttendanceTableManager.Delete(Convert.ToInt64(lblAttKey.Text)))
            {
                lblAttKey.Text = "-1";
                //ddlAttDate_SelectedIndexChanged(sender, e);
                ShowAttendance();
                lblTopMessage.Text = "Deleted attendance listing successfully!";
                lblTopMessage.ForeColor = System.Drawing.Color.Green;
            }
        }
        else
        {
            lblTopMessage.Text = "Please selete a schedule to delete!";
            lblTopMessage.ForeColor = System.Drawing.Color.Green;
        }
    }

    protected void btnPreviousAttDate_Click(object sender, EventArgs e)
    {
        if (ddlAttDate.SelectedIndex == 1)
        {
            btnPreviousAttDate.Enabled = false;
            ddlAttDate.SelectedIndex = ddlAttDate.SelectedIndex - 1;
            ddlAttDate_SelectedIndexChanged(sender, e);
        }
        else if (ddlAttDate.SelectedIndex > 1)
        {
            ddlAttDate.SelectedIndex = ddlAttDate.SelectedIndex - 1;
            ddlAttDate_SelectedIndexChanged(sender, e);
        }
        else
        {
            btnPreviousAttDate.Enabled = false;
        }

        if (ddlAttDate.SelectedIndex != ddlAttDate.Items.Count - 1)
        {
            btnNextAttDate.Enabled = true;
        }
    }

    protected void btnTodayAttDate_Click(object sender, EventArgs e)
    {
        try
        {
            if (ddlAttDate.Items.Count > 0)
            {
                if (ddlAttDate.Text != DateTime.Now.ToString("dd/MMM/yyyy"))
                {
                    ddlAttDate.Text = DateTime.Now.ToString("dd/MMM/yyyy");
                    ddlAttDate_SelectedIndexChanged(sender, e);
                }
            }
        }
        catch (Exception Ex)
        {

        }
    }
    protected void btnNextAttDate_Click(object sender, EventArgs e)
    {
        if (ddlAttDate.SelectedIndex == ddlAttDate.Items.Count - 2)
        {
            btnNextAttDate.Enabled = false;
            ddlAttDate.SelectedIndex = ddlAttDate.SelectedIndex + 1;
            ddlAttDate_SelectedIndexChanged(sender, e);
        }
        else if (ddlAttDate.SelectedIndex != ddlAttDate.Items.Count - 1)
        {
            ddlAttDate.SelectedIndex = ddlAttDate.SelectedIndex + 1;
            ddlAttDate_SelectedIndexChanged(sender, e);
        }
        else
        {
            btnNextAttDate.Enabled = false;
        }

        if (ddlAttDate.SelectedIndex > 1)
        {
            btnPreviousAttDate.Enabled = true;
        }
    }

    protected void btnUpdateTimeTableDetails_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/school/timetable-details.aspx?ttid=" + lblTimeTableKey.Text);
    }
}
